﻿using CommandProcessor.Command;
using Core.Common;
using Data.Infrastructure;
using Data.Repositories;
using Domain.Commands;
using Domain.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.Models;

namespace Domain.Handlers
{
    //Dung de check add
    public class CanAddProductModel : IValidationHandler<CreateOrUpdateProductModelCommand>
    {
        private readonly IProductModelRepository ProductModelRepository;
        public CanAddProductModel(IProductModelRepository ProductModelRepository, IUnitOfWork unitOfWork)
        {
            this.ProductModelRepository = ProductModelRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateProductModelCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.Code == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateProductModelCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateProductModelHandler : ICommandHandler<CreateOrUpdateProductModelCommand>
    {
        private readonly IProductModelRepository ProductModelRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateProductModelHandler(IProductModelRepository ProductModelRepository, IUnitOfWork unitOfWork)
        {
            this.ProductModelRepository = ProductModelRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateProductModelCommand command)
        {
            var ProductModel = new ProductModel
            {
                ID = command.ID,
                Description = command.Description,
                Code = command.Code,
                Author=command.Author,
                Catalogue=command.Catalogue,
                CategoryID=command.CategoryID,
                Date=command.Date,
                Note=command.Note,
                ProductTypeID=command.ProductTypeID,
                Name=command.Name
            };
            if (ProductModel.ID == 0)
                ProductModelRepository.Add(ProductModel);
            else
                ProductModelRepository.Update(ProductModel);

            unitOfWork.Commit();
            return new CommandResult(true,ProductModel.ID);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateProductModelCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateProductModelCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de delete
    public class DeleteProductModelHandler : ICommandHandler<DeleteProductModelCommand>
    {
        private readonly IProductModelRepository ProductModelRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteProductModelHandler(IProductModelRepository ProductModelRepository, IUnitOfWork unitOfWork)
        {
            this.ProductModelRepository = ProductModelRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteProductModelCommand command)
        {
            var ProductModel = ProductModelRepository.GetById(command.ID);
            ProductModelRepository.Delete(ProductModel);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteProductModelCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            ProductModelRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteProductModelCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}

